package org.agent.aigccore.basic.model.response;

import com.google.common.collect.Maps;
import lombok.Getter;
import lombok.Setter;
import org.agent.aigccore.basic.exception.ResultCode;
import org.agent.aigccore.basic.serializable.BaseSerializableObject;
import org.agent.aigccore.utils.StringUtil;

import java.util.HashMap;
import java.util.Map;

@Setter
@Getter
public class CommonResult<T> extends BaseSerializableObject {

    private static final long serialVersionUID = -2508922501757205956L;

    /**
     * 成功状态
     */
    private boolean success = true;

    /**
     * 结果码
     */
    private String resultCode;

    /**
     * 结果描述
     */
    private String resultDesc;

    /**
     * 扩展参数
     */
    protected Map<String, String> externalData = new HashMap<>();

    /**
     * 泛型参数
     */
    private T value;

    /**
     * default constructor
     */
    public CommonResult() {
    }

    /**
     * 构造方法
     */
    public CommonResult(boolean success, ResultCode resultCode
            , String resultDesc, Map<String, String> externalData, T value) {
        this.success = success;
        this.resultCode = resultCode.getCode();
        this.resultDesc = resultDesc;
        this.externalData = externalData;
        this.value = value;
    }

    public static <T> CommonResult<T> ofSuccess() {
        return ofSuccess("处理成功");
    }

    /***
     * 构建成功结果
     *
     * @param msg
     * @return CommonResult
     */
    public static <T> CommonResult<T> ofSuccess(String msg) {
        return new CommonResult<>(true, ResultCode.SUCCESS, msg, null, null);
    }

    /***
     * 构建成功结果
     *
     * @param value 泛型参数
     * @param <T> 泛型
     * @return CommonResult
     */
    public static <T> CommonResult<T> ofSuccess(T value) {
        return new CommonResult<>(true, ResultCode.SUCCESS, ResultCode.SUCCESS.getDesc(), null, value);
    }

    /***
     * 构建成功结果
     *
     * @param value 泛型参数
     * @param <T> 泛型
     * @return CommonResult
     */
    public static <T> CommonResult<T> ofSuccess(T value, Map<String, String> ext) {
        return new CommonResult<>(true, ResultCode.SUCCESS, ResultCode.SUCCESS.getDesc(), ext, value);
    }

    /**
     * 构建失败结果（带扩展信息）
     *
     * @param resultCode
     * @param errorDesc
     * @param externalData
     * @return CommonResult
     */
    public static <T> CommonResult<T> ofFailed(ResultCode resultCode, String errorDesc, Map<String, String> externalData) {
        return new CommonResult<>(false, resultCode, errorDesc, externalData, null);
    }

    /**
     * 直接添加至externalData
     *
     * @param key
     * @param value
     */
    public void addExt(String key, String value) {
        if (StringUtil.isNotBlank(value)) {
            if (null == externalData) {
                externalData = Maps.newHashMap();
            }
            externalData.put(key, value);
        }
    }

    /**
     * 直接添加ALL至externalData
     *
     * @param ext
     */
    public void addAllExt(Map<String, String> ext) {
        if (null == externalData) {
            externalData = Maps.newHashMap();
        }
        externalData.putAll(ext);
    }

    /**
     * 根据KEY获取VALUE
     *
     * @param key
     * @return
     */
    public String getExt(String key) {
        if (null == externalData) {
            return null;
        }
        return externalData.get(key);
    }
}
